package com.xuan.shop.service.impl;

import com.xuan.shop.config.RabbitMqConfig;
import com.xuan.shop.dao.IPrivsUserDao;
import com.xuan.shop.dao.IUserDao;
import com.xuan.shop.entity.Privs;
import com.xuan.shop.entity.User;
import com.xuan.shop.exception.IncorrectUsernameAndPasswordException;
import com.xuan.shop.exception.IncorrectUsernameException;
import com.xuan.shop.exception.UsernameIsExistException;
import com.xuan.shop.exception.UsernameNotNullException;
import com.xuan.shop.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao dao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public void registUser(User user) {
        if(user==null||user.getUsername()==null||"".equals(user.getUsername())){
            throw new UsernameNotNullException("用户名为空");
        }
        if(!isAvailableUserName(user.getName())){
            rabbitTemplate.convertAndSend(RabbitMqConfig.TOPIC_EXCHANGE,"user.topic.register",user);
            log.debug("生产者生产user对象"+user);
        }else{
            throw new UsernameIsExistException("用户名已存在");
        }

    }

    @RabbitListener(queues = {"queue_a","queue_b"})
    @Override
    public void save(User user) {
        dao.save(user);
        log.info("消费者消费user存入数据库"+user);
    }

    @Override
    public void deleteById(int id) {
        dao.deleteById(id);
    }

    @Override
    public void update(User user) {
        dao.save(user);
    }

    @Override
    public List<User> findAll() {
        return dao.findAll();
    }

    @Override
    public User login(String username, String password) {
        if(username==null || "".equals(username)) throw new UsernameNotNullException("用户名为空");

        if(!isAvailableUserName(username)){
            throw new IncorrectUsernameException("用户名不存在");
        }
        User user = dao.findUsersByUsernameAndPassword(username, password);
        if(user==null){
            throw new IncorrectUsernameAndPasswordException("无效的用户名|密码");
        }
        return user;
    }

    @Override
    public boolean isAvailableUserName(String username) {
        if(stringRedisTemplate.keys("usernames").isEmpty()){
            log.debug("当前缓存为空");
            List<String> list=dao.findAllUserName();
            String[] strings=list.toArray(new String[list.size()]);
            stringRedisTemplate.opsForSet().add("usernames",strings);
        }
        return stringRedisTemplate.opsForSet().isMember("usernames",username);
    }

}
